{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Putting it All Together"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn import datasets\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "#load the classifying models\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVC\n",
    "\n",
    "iris = datasets.load_iris()\n",
    "X = iris.data[:, :2]  #load the first two features of the iris data \n",
    "y = iris.target #load the target of the iris data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "knn_3_clf = KNeighborsClassifier(n_neighbors = 3)\n",
    "knn_5_clf = KNeighborsClassifier(n_neighbors = 5)\n",
    "\n",
    "knn_3_scores = cross_val_score(knn_3_clf, X_train, y_train, cv=10)\n",
    "knn_5_scores = cross_val_score(knn_5_clf, X_train, y_train, cv=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "knn_3 mean scores:  0.798333333333 knn_3 std:  0.0908142181722\n",
      "knn_5 mean scores:  0.806666666667  knn_5 std:  0.0559320575496\n"
     ]
    }
   ],
   "source": [
    "print \"knn_3 mean scores: \", knn_3_scores.mean(), \"knn_3 std: \",knn_3_scores.std()\n",
    "print \"knn_5 mean scores: \", knn_5_scores.mean(), \" knn_5 std: \",knn_5_scores.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(4, 0.85111111111111115),\n",
       " (7, 0.82611111111111113),\n",
       " (6, 0.82333333333333347),\n",
       " (5, 0.80666666666666664),\n",
       " (3, 0.79833333333333334),\n",
       " (8, 0.79833333333333334)]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_scores = []\n",
    "for n_neighbors in range(3,9,1):\n",
    "    knn_clf = KNeighborsClassifier(n_neighbors = n_neighbors)\n",
    "    all_scores.append((n_neighbors, cross_val_score(knn_clf, X_train, y_train, cv=10).mean()))\n",
    "sorted(all_scores, key = lambda x:x[1], reverse = True) "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
